<?php

/**
 * This is the model class for table "coupon".
 *
 * The followings are the available columns in table 'coupon':
 * @property string $coupon_id
 * @property string $code
 * @property string $value
 * @property string $from_date
 * @property string $to_date
 * @property string $order_id
 * @property string $user_id
 */
class Coupon extends CActiveRecord
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Coupon the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'coupon';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('code, value', 'required'),
            array('code', 'length', 'max' => 255),
            array('value', 'length', 'max' => 20),
            array('from_date, to_date, order_id, user_id', 'length', 'max' => 10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('coupon_id, code, value, from_date, to_date, order_id, user_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'coupon_id' => ProductModule::t('Id', 'coupon'),
            'code' => ProductModule::t('Code', 'coupon'),
            'value' => ProductModule::t('Value', 'coupon'),
            'from_date' => ProductModule::t('From Date', 'coupon'),
            'to_date' => ProductModule::t('To Date', 'coupon'),
            'order_id' => ProductModule::t('Order', 'coupon'),
            'user_id' => ProductModule::t('User', 'coupon'),
            'is_used' => ProductModule::t('Is Used', 'coupon'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('coupon_id', $this->coupon_id, true);
        $criteria->compare('code', $this->code, true);
        $criteria->compare('value', $this->value, true);
        $criteria->compare('from_date', $this->from_date, true);
        $criteria->compare('to_date', $this->to_date, true);
        $criteria->compare('order_id', $this->order_id, true);
        $criteria->compare('user_id', $this->user_id, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function generateCoupon($amount, $value, $from_date, $to_date)
    {
        for ($i = 0; $i < $amount; $i++) {
            $coupon = new Coupon;
            $coupon->code = $this->_getRandomCode();
            $coupon->value = $value;
            $coupon->from_date = strtotime($from_date);
            $coupon->to_date = strtotime($to_date);
            $coupon->save();
        }
    }

    private function _getRandomCode()
    {
        $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $code = "";
        for ($i = 0; $i < 10; $i++) {
            $code .= $chars[mt_rand(0, strlen($chars) - 1)];
        }

        return $code;
    }

    public function isUsed()
    {
        if (is_null($this->order_id)) {
            return false;
        }
        return true;
    }

}